<?php

 
namespace RedMVC\Router;
use RedMVC;

/**
 * Standard class
 *  
 * @category    RedMVC
 * @package     Router
 * @author      Jan Fischer, bitWorking <info@bitworking.de>
 */
class Standard extends ARouter{

    public function readUrl(){
        $config         = RedMVC\Registry::get('RedMVC_Config');

        $pathInfo       = pathinfo($_SERVER['SCRIPT_NAME']);
        $dirName        = $pathInfo['dirname'];
        $dirName        = str_replace('/index.php', '', $dirName);
        $requestUri     = $_SERVER['REQUEST_URI'];
        $requestString  = substr($requestUri, strlen($dirName));
        
        $this->requestString = ltrim($requestString, '/');
        
        // controller
        if(isset($_GET['controller'])){
            $this->controllerName = $_GET['controller'];
            unset($_GET['controller']);
        }
        else{
            $this->controllerName = $config->defaultController;
        }
        
        // action
        if(isset($_GET['action'])){
            $this->actionName = $_GET['action'];
            unset($_GET['action']);
        }
        else{
            $this->actionName = $config->defaultAction;
        }
        
        // translate
        $this->controllerName   = $this->__($this->controllerName);
        $this->actionName       = $this->__($this->actionName);
        
        // params
        $this->params = $_GET;
    }
    
    public function buildUrl($controller, $action, $params = array()){
        $paramsString = '';
        if(count($params) > 0){
            $paramsString = '&'.http_build_query($params);
        }
        // translate
        $controller = $this->_($controller);
        $action     = $this->_($action);        
        return "/index.php?controller=$controller&action=$action".$paramsString;
    }
}